package b1.scenes.home.picpane
{
	import b1.conifg.*;
	import b1.controls.basic.*;
	import b1.net.*;
	import b1.players.*;
	import b1.scenes.*;
	import b1.scenes.home.*;
	import b1.utils.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.ColorMatrixFilter;
	import flash.net.*;
	import flash.text.*;
	import flash.ui.Mouse;
	import flash.utils.Timer;

	public class PlayerPicIcon extends Sprite
	{
		private var playerInfo:PlayerInfo;
		private var paneTip:PlayerMessage;
		public var mouseOverFlag:Boolean;
		private var showSecond:int;
		private var timer:Timer;
		
		public function PlayerPicIcon(playerInfo:PlayerInfo)
		{
			this.playerInfo = playerInfo;
			super();
			this.mouseOverFlag = false;
			this.showSecond = 0;
			//背景框
			var rankInfo:RankConfig = playerInfo.getRankInfo();
			var UserPicPaneBgr:Class = ResourceHelper.getInstance().getClassByName(rankInfo.userPaneImage);
			var userPicPaneBgr:Sprite = new UserPicPaneBgr();
			addChild(userPicPaneBgr);
			//人人用户头像
			if (playerInfo.renrenId > 0)
			{
				var rrApiHelper:RRApiHelper = new RRApiHelper();
				rrApiHelper.getUserPic(playerInfo.renrenId, onUserPicComplete);
			}
			this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			this.addEventListener(MouseEvent.CLICK, onMouseClick);
		}
		
		private function onUserPicComplete(rrTinyUrl:String, rrUserName:String):void
		{
			//用户图片
			var pictLdr:Loader = new Loader();
			var pictURLReq:URLRequest = new URLRequest(rrTinyUrl);
			pictLdr.load(pictURLReq);
			pictLdr.x = 10;
			pictLdr.y = 10;
			addChild(pictLdr);
			//用户名字
			var userName:TextField = new TextField();
			userName.selectable = false;
        	userName.autoSize = TextFieldAutoSize.LEFT;
			userName.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000);
			userName.text = rrUserName.substr(0,4);
			userName.x = (62 - userName.width) / 2;
			userName.y = 55;
			addChild(userName);
		}
		
		private function onMouseClick(event:MouseEvent):void
		{
			if (SceneContainer.getInstance().mouseDisableFlag && !SceneContainer.getInstance().checkOnlyMousePosition(event.currentTarget))
				return;
			var home:Home = SceneContainer.getInstance().currentScene as Home;
			home.switchLiveId(playerInfo.id);
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			this.filters = [new ColorMatrixFilter(BFont.brightFilter)];
			this.mouseOverFlag = true;
			if (paneTip == null)
			{
				paneTip = new PlayerMessage(playerInfo);
				paneTip.x = this.x + this.width;
				paneTip.y = this.y;
				this.parent.addChild(paneTip);
				timer = new Timer(50);
				timer.addEventListener(TimerEvent.TIMER, onTick);
				timer.start();
			}
			this.showSecond = 2;
		}
		
		private function onTick(event:Event):void
		{
			if (mouseOverFlag || (this.paneTip != null && this.paneTip.mouseOverFlag))
			{
				this.showSecond = 2;		
			}
			else
			{
				if (this.showSecond > 0)
				{
					this.showSecond--;
				}
				else
				{
					removeTip();
				}
			}
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			this.mouseOverFlag = false;
			this.filters = null;
		}
		
		public function removeTip():void
		{
			this.timer.stop();
			this.timer.removeEventListener(TimerEvent.TIMER, onTick);
			if (null != paneTip && null != paneTip.parent)
        	{
        		paneTip.parent.removeChild(paneTip);
        		paneTip = null;
        	}
		}
	}
}